package controller

import (
	"frame/controller/ControllerModule"
	"frame/controller/ControllerModule/ControllerParam"
	"frame/logger/HLogger"
	"frame/logic/LogicModule"
	"frame/controller/ControllerModule/ModelAndView"
	"fmt"
)

var loginlogger = HLogger.GetHLogger("controller.login")

var LoginControllerModule = ControllerModule.ControllerModule{
	Name:"loginController",
	Path:"/login",
	Subpath:map[string]interface{}{
		"/login":login,
		"/logout":logout,
		"/mainpage":mainpage,
	}}

func login(respwriter ControllerParam.HttpRequestWriter,req ControllerParam.HttpRequest,pathparam ControllerParam.PathParameters,txnContext ControllerParam.TxnContext,body ControllerParam.RequestBody){
	loginlogger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"访问login开始")
	respwriter.Write([]byte("login["+txnContext.Txnid+"]"))
	loginlogger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"访问login结束")
}

func logout(req ControllerParam.HttpRequest,respwriter ControllerParam.HttpRequestWriter,pathparam ControllerParam.PathParameters, body ControllerParam.RequestBody,txnContext ControllerParam.TxnContext){
	loginlogger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"访问logout开始")
	respwriter.Write([]byte("logout["+txnContext.Txnid+"]"))
	loginlogger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"访问logout结束")
}
func mainpage(req ControllerParam.HttpRequest,respwriter ControllerParam.HttpRequestWriter,txnContext ControllerParam.TxnContext)(ModelAndView.ModelAndView){
	//进入日志
	loginlogger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"访问mainpage开始")
	//组装上下文
	var contextMap map[string]interface{} = make(map[string]interface{})
	contextMap["id"]=5556
	//调用logic层
	result,err:=LogicModule.ProcessLogic("LoginLogic",HLogger.TxnContext(txnContext),contextMap)
	if err != nil {
		logger.ErrorWithTxnContext(HLogger.TxnContext(txnContext),"处理失败，错误原因["+err.Error()+"]")
		result = make(map[string]interface{})
	}else{
		logger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"处理成功，结果为["+fmt.Sprint(result)+"]")
	}
	//组装返回结果
	var mava = make(map[string]interface{})
	mava["a"]="aaa"
	mava["b"]="bbb"
	for k,v := range result.(map[string]interface{}){mava[k]=v}
	var mv = ModelAndView.CreateModelAndView("a",mava)

	//离开日志
	loginlogger.InfoWithTxnContext(HLogger.TxnContext(txnContext),"访问mainpage结束")
	return mv
}